package cn.phecda.boot.data.test.mybatis;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p> TODO </p>
 *
 * @author : wanpinwei
 */
@RequestMapping("/persons/mybatis")
@RestController
@Transactional
@RequiredArgsConstructor
public class PersonMybatisController {
    private static int index = 0;
    private final PersonMybatisService personMybatisService;


    @GetMapping
    public List<PersonMybatisEntity> all() {
        return personMybatisService.findAll();
    }

    @GetMapping("/id")
    public PersonMybatisEntity id(@RequestParam("id")  String id) {
        return personMybatisService.findById(id);
    }

    @PostMapping
    public PersonMybatisEntity save(@RequestParam("name") String name) {
        String newName = name + ++index;
        return personMybatisService.save(newName);
    }

    @PutMapping
    public void update(@RequestParam("id") String id, @RequestParam("name") String name) {
        personMybatisService.update(id, name);
    }

    @PostMapping("/async")
    public void asnyc(@RequestParam("name") String name) {
        String newName = name + ++index;
        personMybatisService.asyncSave(newName);
    }

    @DeleteMapping
    public void delete(@RequestParam("id")  String id) {
        personMybatisService.delete(id);
    }
}
